<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="130px">
      <!-- <el-form-item label="Customer Name" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="Register Mobile" prop="registerMobile">
        <el-input v-model="queryParams.registerMobile" placeholder="" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="Customer No." prop="custNo">
        <el-input v-model="queryParams.custNo" placeholder="" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="BVN" prop="bvn">
        <el-input v-model="queryParams.bvn" placeholder="" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Account Number" prop="accountsAccountNumber">
        <el-input v-model="queryParams.accountsAccountNumber" placeholder="" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">Re-set</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="accountsList" @selection-change="handleSelectionChange">
      <el-table-column label="#" align="center" type="index"/>
      <el-table-column label="Customer No" align="center" prop="custNo"/>
      <el-table-column label="Register Mobile" align="center" prop="registerMobile"/>
      <el-table-column label="Customer Name" align="center" prop="customerName"/>
      <el-table-column label="BVN" align="center" prop="bvn"/>
      <el-table-column label="channel" align="center" prop="channel"/>
      <el-table-column label="Bank name" align="center" prop="accountsBankName"/>
      <el-table-column label="Account number" align="center" prop="accountsAccountNumber"/>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

  </div>
</template>

<script>
import {addAccounts, delAccounts, exportAccounts, getAccounts, listAccounts, updateAccounts} from "@/api/custservice/accounts";

export default {
  name: "Accounts",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // monnify虚拟账户表格数据
      accountsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        registerMobile: null,
        pageNum: 1,
        pageSize: 10,
        usrId: null,
        custNo: null,
        contractCode: null,
        accountReference: null,
        accountName: null,
        currencyCode: null,
        customerEmail: null,
        customerName: null,
        accountsBankCode: null,
        accountsBankName: null,
        accountsAccountNumber: null,
        accountsAccountName: null,
        collectionChannel: null,
        reservationReference: null,
        reservedAccountType: null,
        channel:null,
        status: null,
        createdOn: null,
        bvn: null,
        restrictPaymentSource: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [{
          required: true,
          message: "创建时间不能为空",
          trigger: "blur"
        }],
        updateTime: [{
          required: true,
          message: "更新时间不能为空",
          trigger: "blur"
        }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询monnify虚拟账户列表 */
    getList() {
      this.loading = true;
      listAccounts(this.queryParams).then(response => {
        this.accountsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        registerMobile: null,
        id: null,
        usrId: null,
        custNo: null,
        contractCode: null,
        accountReference: null,
        accountName: null,
        currencyCode: null,
        customerEmail: null,
        customerName: null,
        accountsBankCode: null,
        accountsBankName: null,
        accountsAccountNumber: null,
        accountsAccountName: null,
        collectionChannel: null,
        reservationReference: null,
        reservedAccountType: null,
        channel:null,
        status: "0",
        createdOn: null,
        bvn: null,
        restrictPaymentSource: null,
        state: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加monnify虚拟账户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccounts(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改monnify虚拟账户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccounts(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccounts(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除monnify虚拟账户编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delAccounts(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有monnify虚拟账户数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportAccounts(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
